package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.adapter.GongGaoAdapter;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.entity.GongGaoEntity;
import com.sbs.gaoxinqutoilet.entity.MainMenuNews;
import com.sbs.gaoxinqutoilet.eventbus.StartNewFragmentEvent;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2020/4/26.
 * Time: 2020/4/26  10:08
 */
public class GongGaoListFragment extends BaseBackFragment {

    public static String NEWS_Key = "gonggao_list";
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_add)
    ImageView toolbarIvAdd;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.gonggao_recyclerview)
    RecyclerView gonggaoRecyclerview;
    private MainMenuNews mainMenuNews;
    private GongGaoAdapter applyRvAdapter;
    private List<GongGaoEntity.DataBean> news_gonggao;
    Unbinder unbinder;


    public static GongGaoListFragment newInstance(MainMenuNews newsId) {
        Bundle args = new Bundle();
        args.putSerializable(NEWS_Key, newsId);
        GongGaoListFragment fragment = new GongGaoListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gonggaolist, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();

        return attachToSwipeBack(view);
    }

    private void initView() {

        initToolbarBack(toolbar);
        toolbarTitle.setText("更多公告");
        toolbarBack.setVisibility(View.VISIBLE);

        Bundle arguments = getArguments();
        if (arguments != null) {
            mainMenuNews = (MainMenuNews) arguments.getSerializable(NEWS_Key);
            news_gonggao = mainMenuNews.getNews_gonggao();
        }


        applyRvAdapter = new GongGaoAdapter(R.layout.item_gonggao, news_gonggao);
        gonggaoRecyclerview.setLayoutManager(new LinearLayoutManager(_mActivity));
        gonggaoRecyclerview.setAdapter(applyRvAdapter);


        applyRvAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GongGaoEntity.DataBean dataBean = news_gonggao.get(position);
                EventBus.getDefault().post(new StartNewFragmentEvent(GongGaoFragment.newInstance(dataBean.getNt_id())));

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

        //EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();

    }
}
